var HttpReq;
var ParseResult;

function IOLAjax(Parser)
{
	this.Debug	= false;
	this.Url	= '';
	this.Data	= '';
	
	ParseResult = Parser;
	if (window.XMLHttpRequest) {
		HttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	HttpReq.onreadystatechange = _process;
	this.PostData = _build_form;
	this.AddPostData = _add_post_data;
	this.SendGet = _send_get;
	this.SendPost = _send_post;
	this.AddDebug = _debug;
}

function _debug(text)
{
	if (this.Debug)
	{
		alert(text);
	}
}

function _build_form(form)
{
	if (!form.name)
	{
		form = document.getElementById(form);
	}
	
	this.AddDebug('_buildForm('+form.name+')');
	
	var data='';
	var add=false;
	
	for (i=0; i<form.elements.length; i++) {
		add = false;
		if (form.elements[i].type=='checkbox') {
			add = true;
			if (form.elements[i].checked) {
				if (form.elements[i].value) {
					value = form.elements[i].value;
				} else {
					value = 1;
				}
			} else {
				value = 0;
			}
		} else if (form.elements[i].type=='radio') {
			if (form.elements[i].checked) {
				add = true;
				value = form.elements[i].value;
			}
		} else {
			add = true;
			value = encodeURIComponent(form.elements[i].value);
		}
		if (add) {
			if (data.length>0) {
				data = data + '&';
			}
			data = data + form.elements[i].name + '=' + value;
		}
	}
	if (data.length>0) {
		data = data + '&';
	}
	data = data + 'form=' + form.name;
	
	this.Data = data;
}

function _add_post_data(name, value)
{
	this.AddDebug('_add_post_data :: '+name+'='+value);
	if (this.Data)
	{
		this.Data = this.Data + '&';
	}
	this.Data = this.Data + name + '=' + value;
}

function _send_get()
{
	this.AddDebug('_sendGet');
	HttpReq.open('get', this.Url, true);
	HttpReq.send('');
}

function _send_post()
{
	this.AddDebug('_sendPost');
	this.AddDebug(this.Data);
	HttpReq.open('post', this.Url, true);
	HttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	HttpReq.send(this.Data);
}

function _process()
{	
	if (HttpReq.readyState == 4)
	{
		if (HttpReq.status == 200)
		{
			if (ParseResult != null)
			{
				ParseResult(HttpReq);
			}
			else
			{
				alert('ParseResult not set');
			}
		}
		else
		{
			alert('Not able to retrieve description');
		}
	}
}